package com.snap.vseries.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.base.BaseActivity;
import com.snap.vseries.manager.ConfigManager;
import com.snap.vseries.ui.widget.DialogView;
import com.snap.vseries.ui.widget.TextButton;
import com.snap.vseries.utils.ToastUtils;
import com.snap.vseries.utils.TopTitleUtils;

import java.text.DecimalFormat;

import butterknife.BindView;

/**
 * 荧光校准，结果存在本地
 */
public class AmpCalibrationActivity extends BaseActivity {
    @BindView(R.id.water_bg_tv)
    TextView waterBgTv;
    @BindView(R.id.fam_tv)
    TextView famTv;
    @BindView(R.id.vic_tv)
    TextView vicTv;
    @BindView(R.id.rox_tv)
    TextView roxTv;
    @BindView(R.id.cy5_tv)
    TextView cy5Tv;
    @BindView(R.id.save_setting_btn)
    TextButton saveSettingBtn;
    private TextView[] tvs;

    /**
     * 荧光校准
     */
    private float[] coeffs;
    /**
     * 荧光校准
     */
    private float[] coeffsCopy;

    @Override
    protected int layoutResId() {
        return R.layout.activity_amp_calibration;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();

    }

    private void initData() {
        coeffs = ConfigManager.getInstance().getSettingInfo().getCoefficients();
        if (coeffs == null) {
            coeffs = new float[]{0, 0, 0, 0, 0};
        }
        coeffsCopy = coeffs.clone();
        if (coeffsCopy == null || coeffsCopy.length == 0) {
            coeffsCopy = new float[]{0, 0, 0, 0, 0};
        }
    }

    private void initView() {
        new TopTitleUtils(this).setTitle("荧光校准").setLeft(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvs = new TextView[]{famTv, vicTv, roxTv, cy5Tv, waterBgTv};
        for (int i = 0; i < tvs.length; i++) {
            int index = i;
            tvs[index].setText(String.valueOf(coeffsCopy[index]));
            tvs[index].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new DialogView(AmpCalibrationActivity.this)
                            .setTitle("请输入数值")
                            .setContentType(false)
                            .setEditContent(tvs[index].getText().toString())
                            .setOnClickListener(content -> {
                                try {
                                    content = new DecimalFormat("0.###").format(Double.valueOf(content));
                                } catch (NumberFormatException e) {
                                    ToastUtils.get().showText("输入的值不符合规范");
                                    return;
                                }
                                tvs[index].setText(content);
                                coeffsCopy[index] = Float.parseFloat(content);
                            }).show();
                }
            });
        }
        saveSettingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DialogView(AmpCalibrationActivity.this)
                        .setTitle("确定修改数值吗")
                        .setTextContent("修改数值")
                        .setOnClickListener(content -> {
                            ConfigManager.getInstance().getSettingInfo().setCoefficients(coeffsCopy);
                        }).show();
            }
        });
    }

}
